package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cqut.dto.LimitShowDTO;
import com.cqut.service.IAwardsSelectionService;
import com.cqut.util.ResponseUtil;

@RestController
@RequestMapping("/api/awardsSelection")
public class AwardsSelectionController {
	@Resource(name="awardsSelectionService")
	private IAwardsSelectionService awardsSelectionService;
	
	/**
	 * 初始化大赛列表
	 * @param limitShowDTO
	 * @param errors
	 * @return
	 */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
	public ResponseEntity<String> getList(
			String competeName,
			@Validated LimitShowDTO limitShowDTO,			
            BindingResult errors){ 
        if(errors.hasErrors()) {
	            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
    	}
	    Map<String, Object> result = new HashMap<String, Object>();
	    List<Map<String, Object>> list = awardsSelectionService.getList(limitShowDTO, competeName);
    	long count = awardsSelectionService.findCountOfList(competeName);
    	
	    result.put("rows", list);
        result.put("total", count);
    	
	    return ResponseUtil.getResEntityForGetAndJson(result);
	 }
    
    /**
	 * 初始化阶段列表
	 * @param limitShowDTO
	 * @param errors
	 * @return
	 */
    @RequestMapping(value = "/getStateList", method = RequestMethod.GET)
	public ResponseEntity<String> getStateList(
			String stateName,
			String competitionId,
			@Validated LimitShowDTO limitShowDTO,			
            BindingResult errors){ 
        if(errors.hasErrors()) {
	            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
    	}
	    Map<String, Object> result = new HashMap<String, Object>();
	    List<Map<String, Object>> list = awardsSelectionService.getStateList(limitShowDTO, stateName, competitionId);
	    long count = awardsSelectionService.findCountOfStateList(stateName, competitionId);
    	
	    result.put("rows", list);
        result.put("total", count);
        
	    return ResponseUtil.getResEntityForGetAndJson(result);
	 }
	
	/**
	 * 得到信息列表
	 * */
	@RequestMapping(value="/getDetailList", method=RequestMethod.GET)
	public ResponseEntity<String> getDetailList(
			String teamName,
			String comProcessId,
			@Validated
			LimitShowDTO limitShowDTO, BindingResult errors) {
		 if(errors.hasErrors())
	         return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		 
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> list = awardsSelectionService.getDetailList(teamName, limitShowDTO);
		long count = awardsSelectionService.getCountOfDetail(teamName);
		
		result.put("total", count);
		result.put("rows", list);
		return ResponseUtil.getResEntityForGetAndJson(result);
	}
	
	/**
	 * 阶段名称列表
	 * */
    @RequestMapping(value="/getState", method=RequestMethod.GET)
    public ResponseEntity<String> getState() {
    	Map<String, Object> result = new HashMap<String, Object>();
        List<Map<String, Object>> stateList = awardsSelectionService.getState();
        
        result.put("rows", stateList);
        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    /**
	 * 赛制流程列表
	 * */
    @RequestMapping(value="/getFlow", method=RequestMethod.GET)
    public ResponseEntity<String> getFlow() {
    	Map<String, Object> result = new HashMap<String, Object>();
        List<Map<String, Object>> flowList = awardsSelectionService.getFlow();
        
        result.put("rows", flowList);
        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
}
